// WARNING: This file has been generated. Any manual changes must be made within preserved regions or they will be lost.

//===============================================================================
//
// CommonFunctionsTests
//
// PURPOSE: 
// Tests the functionality of CommonFunctions.
//
// NOTES: 
// 
//
//===============================================================================
//
// Copyright (C) 2003 Wallis Software Solutions
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//
//===============================================================================

using System;
using NUnit.Framework;

namespace Agile.Common.Tests
{
    /// <summary>
    /// Tests the functionality of CommonFunctions.
    /// </summary>
    [TestFixture]
    public class CommonFunctionsTests
    {
        private const string _negativeNumber = "-932";

        /// <summary>
        /// Just used to test SafeString functionality.
        /// </summary>
        private class SafeStringTester
        {
            private const string _info = "abcd";

            /// <summary>
            /// Instantiate
            /// </summary>
            /// <returns></returns>
            public static SafeStringTester Instantiate()
            {
                return new SafeStringTester();
            }

            /// <summary>
            /// Just return the info.
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return _info;
            }
        }

        /// <summary>
        /// Verifies that the IsNumeric functions returns the correct results.
        /// </summary>
        [Test]
        public void AreAllCharactersNumeric()
        {
            // Check 1234567890 returns true
            Assert.IsTrue(CommonFunctions.AreAllCharactersNumeric("1234567890"), "Check 1234567890 returns true");
            // Check q123245 returns false
            Assert.IsFalse(CommonFunctions.AreAllCharactersNumeric("q123245"), "Check q123245 returns false");
            // Check shfk returns false
            Assert.IsFalse(CommonFunctions.AreAllCharactersNumeric("shfk"), "Check shfk returns false");
            // Check _ returns false
            Assert.IsFalse(CommonFunctions.AreAllCharactersNumeric("_"), "Check _ returns false");
            // Check - returns false
            Assert.IsFalse(CommonFunctions.AreAllCharactersNumeric("-"), "Check - returns false");
            // Check -932 returns false
            Assert.IsFalse(CommonFunctions.AreAllCharactersNumeric(_negativeNumber), "Check -932 returns false");
        }

        /// <summary>
        /// Verifies that the IsAlphaNumeric functions returns the correct results.
        /// </summary>
        [Test]
        public void IsAlphanumericTest()
        {
            // Check 1234567890 returns true
            Assert.IsTrue(CommonFunctions.IsAlphanumeric("1234567890"), "Check 1234567890 returns true");
            // Check 7abcdefg1hijklmnopqrs2tuvw890xyz3245 returns true
            Assert.IsTrue(CommonFunctions.IsAlphanumeric("7abcdefg1hijklmnopqrs2tuvw890xyz3245"),
                          "Check 7abcdefg1hijklmnopqrs2tuvw890xyz3245 returns true");
            // Check _ returns false
            Assert.IsFalse(CommonFunctions.IsAlphanumeric("_"), "Check _ returns false");
            // Check * returns false
            Assert.IsFalse(CommonFunctions.IsAlphanumeric("*"), "Check * returns false");
            // Check # returns false
            Assert.IsFalse(CommonFunctions.IsAlphanumeric("#"), "Check # returns false");
            // Check -932 returns false
            Assert.IsFalse(CommonFunctions.IsAlphanumeric(_negativeNumber), "Check -932 returns false");
        }

        /// <summary>
        /// Verifies that the IsAlpha functions returns the correct results.
        /// </summary>
        [Test]
        public void IsAlphaTest()
        {
            // Check qwertyuioplkjhgfdsazxcvbnm returns true
            Assert.IsTrue(CommonFunctions.IsAlpha("qwertyuioplkjhgfdsazxcvbnm"),
                          "Check qwertyuioplkjhgfdsazxcvbnm returns true");
            // Check 7abcdefg1hijklmnopqrs2tuvw890xyz3245 returns false
            Assert.IsFalse(CommonFunctions.IsAlpha("7abcdefg1hijklmnopqrs2tuvw890xyz3245"),
                           "Check 7abcdefg1hijklmnopqrs2tuvw890xyz3245 returns false");
            // Check _ returns false
            Assert.IsFalse(CommonFunctions.IsAlpha("_"), "Check _ returns false");
            // Check * returns false
            Assert.IsFalse(CommonFunctions.IsAlpha("*"), "Check * returns false");
            // Check # returns false
            Assert.IsFalse(CommonFunctions.IsAlpha("#"), "Check # returns false");
            // Check -932 returns false
            Assert.IsFalse(CommonFunctions.IsAlpha(_negativeNumber), "Check -932 returns false");
            // Check 3 returns false
            Assert.IsFalse(CommonFunctions.IsAlpha("3"), "Check 3 returns false");
        }

        /// <summary>
        /// Verifies that an exception is thrown when Right() is given a null.
        /// </summary>
        [Test, ExpectedException(typeof (ArgumentNullException))]
        public void RightNullExceptionThrown()
        {
            CommonFunctions.Right(null, 5);
        }

        /// <summary>
        /// Verifies Safe decimal doesnt throw exeptions and always returns an decimal
        /// </summary>
        [Test]
        public void SafeDecimalTests()
        {
            // Check SafeDecimal returns 0 for x
            Assert.AreEqual(0, CommonFunctions.SafeDecimal("x"), "Check SafeDecimal returns 0 for x");
            // Check SafeDecimal returns 0 for 0
            Assert.AreEqual(0, CommonFunctions.SafeDecimal(0), "Check SafeDecimal returns 0 for 0");
            // Check SafeDecimal returns 5 for 5
            Assert.AreEqual(5, CommonFunctions.SafeDecimal(5), "Check SafeDecimal returns 5 for 5");
            // Check SafeDecimal returns 5.5 for 5.5
            Assert.AreEqual(5.5, CommonFunctions.SafeDecimal(5.5), "Check SafeDecimal returns 5.5 for 5.5");
            // Check SafeDecimal returns 0 for null
            Assert.AreEqual(0, CommonFunctions.SafeDecimal(null), "Check SafeDecimal returns 0 for null");
            // Check SafeDecimal returns 7.852 for "7.852"
            Assert.AreEqual(7.852, CommonFunctions.SafeDecimal("7.852"), "Check SafeDecimal returns 7.852 for '7.852'");
            // Check SafeDecimal returns -1 for DateTime when default value is -1
            Assert.AreEqual(-1, CommonFunctions.SafeDecimal(DateTime.Now, -1),
                            "Check SafeDecimal returns -1 for DateTime when default value is -1");
            // Check SafeDecimal returns 9.123456789 for 9.123456789 when default value is -2
            Assert.AreEqual(9.123456789, CommonFunctions.SafeDecimal(9.123456789, -2),
                            "Check SafeDecimal returns 9.123456789 for 9.123456789 when default value is -2");
        }

        /// <summary>
        /// Verifies Safe double doesnt throw exeptions and always returns an double
        /// </summary>
        [Test]
        public void SafeDoubleTests()
        {
            // Check SafeDouble returns 0 for x
            Assert.AreEqual(0, CommonFunctions.SafeDouble("x"), "Check SafeDouble returns 0 for x");
            // Check SafeDouble returns 0 for 0
            Assert.AreEqual(0, CommonFunctions.SafeDouble(0), "Check SafeDouble returns 0 for 0");
            // Check SafeDouble returns 5 for 5
            Assert.AreEqual(5, CommonFunctions.SafeDouble(5), "Check SafeDouble returns 5 for 5");
            // Check SafeDouble returns 5.5 for 5.5
            Assert.AreEqual(5.5, CommonFunctions.SafeDouble(5.5), "Check SafeDouble returns 5.5 for 5.5");
            // Check SafeDouble returns 0 for null
            Assert.AreEqual(0, CommonFunctions.SafeDouble(null), "Check SafeDouble returns 0 for null");
            // Check SafeDouble returns 7.852 for "7.852"
            Assert.AreEqual(7.852, CommonFunctions.SafeDouble("7.852"), "Check SafeDouble returns 7.852 for '7.852'");
            // Check SafeDouble returns -1 for DateTime when default value is -1
            Assert.AreEqual(-1, CommonFunctions.SafeDouble(DateTime.Now, -1),
                            "Check SafeDouble returns -1 for DateTime when default value is -1");
            // Check SafeDouble returns 9.123456789 for 9.123456789 when default value is -2
            Assert.AreEqual(9.123456789, CommonFunctions.SafeDouble(9.123456789, -2),
                            "Check SafeDouble returns 9.123456789 for 9.123456789 when default value is -2");
        }

        /// <summary>
        /// Verifies Safe float doesnt throw exeptions and always returns an float
        /// </summary>
        [Test]
        public void SafeFloatTests()
        {
            // Check SafeFloat returns 0 for x
            Assert.AreEqual(0, CommonFunctions.SafeFloat("x"), "Check SafeFloat returns 0 for x");
            // Check SafeFloat returns 0 for 0
            Assert.AreEqual(0, CommonFunctions.SafeFloat(0), "Check SafeFloat returns 0 for 0");
            // Check SafeFloat returns 5 for 5
            Assert.AreEqual(5, CommonFunctions.SafeFloat(5), "Check SafeFloat returns 5 for 5");
            // Check SafeFloat returns 5.5 for 5.5
            Assert.AreEqual(5.5, CommonFunctions.SafeFloat(5.5), "Check SafeFloat returns 5.5 for 5.5");
            // Check SafeFloat returns 0 for null
            Assert.AreEqual(0, CommonFunctions.SafeFloat(null), "Check SafeFloat returns 0 for null");
            // Check SafeFloat returns 7.852 for "7.852"
            Assert.AreEqual(7.852f, CommonFunctions.SafeFloat("7.852"), "Check SafeFloat returns 7.852 for '7.852'");
            // Check SafeFloat returns -1 for DateTime when default value is -1
            Assert.AreEqual(-1, CommonFunctions.SafeFloat(DateTime.Now, -1),
                            "Check SafeFloat returns -1 for DateTime when default value is -1");
            // Check SafeFloat returns 9.123457 for 9.123457 when default value is -2 (6 is rounded up to 7)
            Assert.AreEqual(9.123457f, CommonFunctions.SafeFloat(9.123457, -2),
                            "Check SafeFloat returns 9.123457 for 9.123457 when default value is -2 (6 is rounded up to 7)");
        }

        /// <summary>
        /// Verifies Safe int doesnt throw exeptions and always returns an int
        /// </summary>
        [Test]
        public void SafeIntTests()
        {
            // Check SafeInt returns 0 for x
            Assert.AreEqual(0, CommonFunctions.SafeInt("x"), "Check SafeInt returns 0 for x");
            // Check SafeInt returns 0 for 0
            Assert.AreEqual(0, CommonFunctions.SafeInt(0), "Check SafeInt returns 0 for 0");
            // Check SafeInt returns 5 for 5
            Assert.AreEqual(5, CommonFunctions.SafeInt(5), "Check SafeInt returns 5 for 5");
            // Check SafeInt returns 0 for 5.5
            Assert.AreEqual(0, CommonFunctions.SafeInt(5.5), "Check SafeInt returns 0 for 5.5");
            // Check SafeInt returns 0 for null
            Assert.AreEqual(0, CommonFunctions.SafeInt(null), "Check SafeInt returns 0 for null");
            // Check SafeInt returns 7 for "7"
            Assert.AreEqual(7, CommonFunctions.SafeInt("7"), "Check SafeInt returns 7 for '7'");
            // Check SafeInt returns -1 for DateTime when default value is -1
            Assert.AreEqual(-1, CommonFunctions.SafeInt(DateTime.Now, -1),
                            "Check SafeInt returns -1 for DateTime when default value is -1");
            // Check SafeInt returns 9 for 9 when default value is -2
            Assert.AreEqual(9, CommonFunctions.SafeInt(9, -2), "Check SafeInt returns 9 for 9 when default value is -2");
        }

        /// <summary>
        /// Verifies SafeString returns an empty string when null and a valid string when there is one.
        /// </summary>
        [Test]
        public void SafeStringTests()
        {
            // Check SafeString returns string.Empty for null
            Assert.AreEqual(string.Empty, CommonFunctions.SafeString(null),
                            "Check SafeString returns string.Empty for null");
            // Check SafeString returns string.Empty for 'abc'
            Assert.AreEqual("abc", CommonFunctions.SafeString("abc"), "Check SafeString returns string.Empty for 'abc'");
            // Check SafeStringTester returns 'abcd'
            Assert.AreEqual("abcd", CommonFunctions.SafeString(SafeStringTester.Instantiate()),
                            "Check SafeStringTester returns 'abcd'");
        }
    }
}